##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################

from PyQt4 import QtGui, QtCore, QtWebKit
import lisp.layout as layout
from lisp.ui.qclicklabel import QClickLabel


class LayoutSelect(QtGui.QDialog):

    def __init__(self, parent=None):
        super(LayoutSelect, self).__init__(parent)

        self.setWindowModality(QtCore.Qt.ApplicationModal)
        self.setWindowTitle('Layout selection')
        self.setMaximumSize(675, 375)
        self.setMinimumSize(675, 375)
        self.resize(675, 375)

        self.vLayout = QtGui.QVBoxLayout(self)
        self.vLayout.setMargin(5)

        self.gridLayoutWidget = QtGui.QWidget(self)
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setMargin(0)

        self.comboBox = QtGui.QComboBox(self.gridLayoutWidget)
        self.gridLayout.addWidget(self.comboBox, 0, 0, 1, 1)

        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)

        self.layButton = QtGui.QPushButton(self.gridLayoutWidget)
        self.layButton.setText('Select layout')
        self.layButton.setSizePolicy(sizePolicy)
        self.gridLayout.addWidget(self.layButton, 0, 1, 1, 1)

        self.vLayout.addWidget(self.gridLayoutWidget)

        self.webView = QtWebKit.QWebView(self)
        self.vLayout.addWidget(self.webView)

        self.image = QtGui.QPushButton('Show image')
        self.vLayout.addWidget(self.image)

        for lay in layout.LAYOUT_LIST:
            self.comboBox.addItem(layout.getLayoutClass(lay).LAYOUT_DESCRIPTION_NAME)

        self.showDescription(0)

        self.comboBox.currentIndexChanged.connect(self.showDescription)
        self.layButton.clicked.connect(self.accept)
        self.image.clicked.connect(self.showFullImage)

    def closeEvent(self, e):
        e.ignore()

    def getSlected(self):
        return layout.LAYOUT_LIST[self.comboBox.currentIndex()]

    def showDescription(self, index):
        lay = layout.getLayoutClass(layout.LAYOUT_LIST[index])

        self.pixmap = lay.LAYOUT_DESCRIPTION_IMAGE
        self.webView.setHtml('<html><body style="font-family: default; font-size: 10pt"><h3>' + lay.LAYOUT_DESCRIPTION_NAME
                             + ':</h3><br>' + lay.LAYOUT_DESCRIPTION_TEXT + '</body></html>')

    def showFullImage(self, p):
        fullImage = QtGui.QDialog(self)
        fullImage.setWindowModality(QtCore.Qt.ApplicationModal)
        fullImage.resize(self.pixmap.size())

        label = QClickLabel(fullImage)
        label.setPixmap(self.pixmap)
        label.clicked.connect(fullImage.accept)

        fullImage.exec_()
